//index.js
import http from '../../../class/Utils/Http.js'
import user from '../../../class/Utils/User.js'
import tips from '../../../class/Utils/Tips.js'

Page({
    data: {
        maxlengthshu: '-1',//输入文本的长度限制，-1是无限制
        autofocus: true,
        fasongbtndisabled: true,
        liuyanblock: false,
        liuyantitle: '发表留言',
        liuyanstate: 1,
        tid: 0,
        liuyan_list_state:false
    },
    //事件处理函数
    onLoad: function (options) {
        var tid = options.tid
        var that = this
        that.setData({
            mid:tid
        })
        tips.loading()
        http.req('/syq/trade_detail', {tid: tid}, function (res) {
            console.log(res)
            if (res.type == 2) {
                res.typename = '供应'
                res.typecls = 'gongying'
            }
            if (res.type == 1) {
                res.typename = '求购'
                res.typecls = 'qiugou'
            }
          if (res.type == 3) {
            res.typename = '招商'
            res.typecls = 'zhaoshang'
          }
          if (res.type == 4) {
            res.typename = '商服'
            res.typecls = 'shangfu'
          }

            that.setData({
                info: res
            })
            
        });
       /*
       查看留言
        */
       that.liuyan_list()
      tips.loaded()
    },
    liuyan_list:function () {
        var that = this
        /*
         查看留言
         */
        var ldata = {
            uid:wx.getStorageSync('user').userid,
            mid:that.data.mid
        }
        http.req("/Api/liuyan_list",ldata,function (res) {
            
            if (res.length > 0) {
                that.setData({
                    liuyan_list_state: true
                })
            }
            that.setData({
                liuyan_list:res
            })
        })
    },
    /*
     输入内容
     */
    content:function (e) {
        this.setData({
            content:e.detail.value,
            fasongbtndisabled:false
        })
    },
    liuyan:function (e) {
        var that = this
        var user = wx.getStorageSync("user")
      if (that.data.content == '') {
        tips.error('请输入留言内容')
        return
      }
      that.setData({
        fasongbtndisabled : true
      })
      
        var data = {
            uid:user.userid,
            content:that.data.content,
            mid:that.data.mid
        }
        http.req("/Api/add_liuyan",data,function (res) {
            if(res.code=='202'){
                tips.ok(res.msg_val)
                that.liuyan_list()
                that.setData({
                  liuyanblock:false,
                  fasongbtndisabled: false,
                  content:''
                })
            }else{
                tips.error(res.msg_val)
            }
        })
        console.log(data)
    },

    //去首页
    gotohome: function () {
        wx.switchTab({
            url: '/pages/index/index'
        })
    },
    //打开评论
    pinglunblockopen: function (e) {
        var that = this
        user.log_user(that,function () {
            var foruser = e.currentTarget.dataset.username
            var liuyanblocktag = that.data.liuyanblock

            if (liuyanblocktag == false) {
                that.setData({
                    liuyanblock: true,
                    autofocus: true,
                })
            } else {
                that.setData({
                    liuyanblock: false,
                    foruser: '',
                    liuyantitle: '发表留言',
                    liuyanstate: 1,
                    liuyanforuser: '',
                    autofocus: false,
                })
            }

            if (foruser !== '') {
                that.setData({
                    liuyantitle: '回复留言',
                    liuyanstate: 2,
                    liuyanforuser: foruser,
                })
            } else {
                that.setData({
                    liuyantitle: '发表留言',
                    liuyanstate: 1,
                    liuyanforuser: '',
                })
            }
        })


    },
    //关闭评论区
    pinglunblockclose: function () {
        var that = this
        that.setData({
            liuyanblock: false,
            foruser: '',
            liuyantitle: '发表留言',
            liuyanstate: 1,
            liuyanforuser: '',
            autofocus: false,
        })
    },
    /*
     授权登录
     */
    bindGetUserInfo: function (e) {
        var that = this
        user.user(this, function (res) {
            that.setData({
                shouquan: 0,
                userinfo: res

            })
            if (res.log == 0) {
                that.setData({
                    bangdingtoocletag: true
                })
            }
        })

    },
    /*
     弹出 登录
     */
    bangdingtoocle: function () {
        var that = this
        that.setData({
            bangdingtoocletag: !that.data.bangdingtoocletag
        })
    },
    /*
     点击登录
     */
    denglu: function (e) {
        var that = this
        var zhanghao = e.detail.value.zhanghao
        var mima = e.detail.value.mima
        if (!zhanghao) {
            tips.error("账号不能为空");
            return false
        }
        if (!mima) {
            tips.error("密码不能为空");
        }
        http.req("/Api/log", {login: zhanghao, passwd: mima, uid: wx.getStorageSync('user').userid}, function (res) {
            console.log(res)
            if (res.code == '200') {
                var user = wx.getStorageSync('user')
                user.log = 1
                wx.setStorageSync('user', user)
                tips.ok('登录成功')
                that.setData({
                    bangdingtoocletag: false,
                    shengyihao: zhanghao
                })
            } else {
                tips.error(res.msg_val)
            }
        })
    },
  //长短 介绍切换
  changeintro: function (e) {
    var that = this
    var info = that.data.info
    var datalistzhi = info.changduans

    if (datalistzhi == 1) {
     info.changduans = 0
    }else {
      info.changduans = 1
    }
    that.setData({
      info: info
    })
  },
  /*
    收藏
    */
  shoucang: function (e) {
    var that = this
    /*
     log_user 返回  缓存user
     */
    user.log_user(this, function (res) {
      var info = that.data.info
      var id=that.data.mid
      var data = {
        uid: res.userid,
        tid: id
      }
      http.req("/Api/add_shoucang", data, function (res) {
        if (res.code == '203') {
          tips.ok(res.msg_val);
          info.shoucang = 1
          that.setData({
            info:info
          })
        } else {
          tips.error(res.msg_val)
        }
      })
    })

  },
  onShareAppMessage: function (res) {
   var that=this
    return {
      title: "【"+that.data.info.typename+"】"+that.data.info.title,
      path: '',
      imageUrl:''
    }
  },
  huliuyan:function(){
    
  },

  /*
   点击名片
   */
  mingpian: function (e) {
    var that = this
    user.log_user(this, function (res) {
      var poster = e.currentTarget.dataset.poster
      var cid = e.currentTarget.dataset.cid
      if (cid) {
        var dd = {
          poster: poster,
          cid: cid
        }
        console.log(dd)
        http.reqload("/Api/mingpian", dd, function (res) {
          console.log(res)
          if (res.code == 200) {
            tips.goto('/pages/mingpian/card/card?id=' + res.mid)
          } else {
            tips.error(res.msg_val)
          }
        })
      } else {
        tips.error("暂无名片")
      }

    })
  },
  //点击图片放大
  previewImage: function (e) {
    var that = this;
    console.log(e)


    //获取点击的数据里的图片


    var thatdatalist = that.data.info.pic_list_img
    console.log(thatdatalist)

    var newimgdata = [];
    for (var i = 0; i < thatdatalist.length; i++) {
      if (thatdatalist[i]) {
        newimgdata.push('http://img.i.album.toocle.com/0-0-0/' + thatdatalist[i]);
      }

    }
    that.setData({
      newimgdata: newimgdata,
    });

    console.log(newimgdata)







    var current = e.currentTarget.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: that.data.newimgdata // 需要预览的图片http链接列表  
    })
  },
  //获取绑定微信的手机号码
  getPhoneNumber: function (e) {
    var that = this
    user.getPhoneNumber(e, that)
  },

})
